package com.cr.crsystem.system.controller;


import com.cr.crsystem.common.utils.Result;
import com.cr.crsystem.system.entity.Car;
import com.cr.crsystem.system.service.RentService;
import com.cr.crsystem.system.vo.RentCarVo;
import com.cr.crsystem.system.vo.RentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author su
 * @since 2022-06-21
 */
@CrossOrigin
@Api(tags = "预约管理")
@Slf4j
@RestController
@RequestMapping("/system/rent")
public class RentController {

    @Autowired
    private RentService rentService;


    @ApiOperation("取消预约")
    @PostMapping("/cancel")
    public Result<?> cancel(@RequestBody RentVo rentVo) {
        rentService.cancel(rentVo);
        return Result.OK("操作成功");
    }


    @ApiOperation("完成订单")
    @PostMapping("/finish")
    public Result<?> finish(@RequestBody RentVo rentVo) {
        rentService.finish(rentVo);
        return Result.OK("操作成功");
    }


    @ApiOperation("新增预约")
    @PostMapping("/add")
    public Result<RentVo> add(@RequestBody RentVo rentVo) {
        Result<RentVo> result = new Result<>();
        RentVo addRent = rentService.addRent(rentVo);
        if(addRent==null) return result.error20001("已是车主，预约失败");
        result.setMessage("预约成功");
        result.setResult(addRent);
        return result;
    }

    @ApiOperation("查询个人预约")
    @GetMapping("/queryPersonRent")
    public Result<?> queryPersonRent(@RequestParam(name = "userId", required = true) Integer userId) {
        List<RentCarVo> personRent = rentService.getPersonRent(userId);
        return Result.OK(personRent);
    }


    @ApiOperation("查询个人订单")
    @GetMapping("/queryPersonOrder")
    public Result<?> queryPersonOrder(@RequestParam(name = "userId", required = true) Integer userId) {
        List<RentCarVo> personOrder = rentService.getPersonOrder(userId);
        return Result.OK(personOrder);
    }

}

